<!DOCTYPE html>
<script src="../../resources/js-test.js"></script>
<body>
<p>Test for &lt;<a href="rdar://5483519">rdar://5483519</a>&gt; Pressing Enter on selected buttons should fire onclick</p>
<div id="console"></div>
<div id="container">
<p>In a form:</p>
<form onsubmit="logEvent(event); return false">
    <button type="button" onclick="logEvent(event)">button type="button"</button>
    <button type="submit" onclick="logEvent(event)">button type="submit"</button>
    <button type="reset" onclick="logEvent(event)">button type="reset"</button>
    <input type="checkbox" onclick="logEvent(event)" value="input type='checkbox'">
    <input type="button" onclick="logEvent(event)" value="input type='button'">
    <input type="submit" onclick="logEvent(event)" value="input type='submit'">
    <input type="reset" onclick="logEvent(event)" value="input type='reset'">
</form>
<p>Outside a form:</p>
<button type="button" onclick="logEvent(event)">button type="button"</button>
<button type="submit" onclick="logEvent(event)">button type="submit"</button>
<button type="reset" onclick="logEvent(event)">button type="reset"</button>
<input type="checkbox" onclick="logEvent(event)" value="input type='checkbox'">
<input type="button" onclick="logEvent(event)" value="input type='button'">
<input type="submit" onclick="logEvent(event)" value="input type='submit'">
<input type="reset" onclick="logEvent(event)" value="input type='reset'">
</div>
<script>
function nodeIsChildOfForm(node)
{
    return node.parentNode && node.parentNode.tagName == 'FORM';
}

function targetDescription(node)
{
    return node.nodeName + (node.type ? ' type=' + node.type : '') + (nodeIsChildOfForm(node) ? ' in form' : '');
}

function logEvent(event)
{
    debug(event.type + ': ' + targetDescription(event.target));
}

var keys = ['Enter', ' '];
var keyNames = ['Enter', 'U+0020'];
var tagNames = ['button', 'input'];

for (var i in keys) {
    debug('');
    debug('');
    debug('Sending ' + keyNames[i] + ' keypresses...');
    debug('');
    for (var j in tagNames) {
        var elements = document.getElementsByTagName(tagNames[j]);
        debug('');
        debug('Looping over ' + elements.length + ' ' + tagNames[j] + ' elements...');
        debug('');
        for (var k = 0; k < elements.length; ++k) {
            elements[k].focus();
            eventSender.keyDown(keys[i], []);
        }
    }
}
document.querySelector('#container').remove();
</script>
</body>
